package com.pine.dbant.plugin.mysql;

import com.pine.dbant.common.model.ConnectionInfo;
import com.pine.dbant.common.model.JdbcMetadataTreeView;
import com.pine.dbant.common.plugin.AbstractJdbcDatabasePlugin;
import com.pine.dbant.common.plugin.PluginType;
import com.pine.dbant.common.plugin.annotion.Plugin;
import com.pine.dbant.common.plugin.meta.FunctionRegister;
import com.pine.dbant.common.store.CacheStore;
import com.pine.dbant.plugin.mysql.function.*;
import com.pine.sunflower.core.plugin.Configuration;
import com.pine.sunflower.core.utils.Strings;
import org.apache.commons.lang3.StringUtils;

import java.util.Arrays;


/**
 * @author xiaoyuan
 * @create 2022/11/8 16:06
 **/
@Plugin(id = "mysql", type = PluginType.DATABASE,javascriptFilename = "mysql.1.0.0.iife.js")
public class MysqlDatabasePlugin extends AbstractJdbcDatabasePlugin {

    public MysqlDatabasePlugin(Configuration configuration, CacheStore cacheStore) {
        super(configuration,cacheStore);
    }

    @Override
    public String concatJdbcUrl(ConnectionInfo connectionInfo) {
        return Strings.format("jdbc:mysql://{}:{}/{}{}", connectionInfo.getHost(),
                connectionInfo.getPort(), StringUtils.isNotBlank(connectionInfo.getDbName()) ? connectionInfo.getDbName() : "",
                "");
    }

    @Override
    public FunctionRegister functionRegister() {
        return config -> config.addFunction(Arrays.asList(
                new MysqlCatalogFunction(JdbcMetadataTreeView.CONN.getType()),
                new MysqlTablesFunction(JdbcMetadataTreeView.TABLE_TYPE.getType()),
                new MysqlQueryFunctionsFunction(JdbcMetadataTreeView.FUNCTION_TYPE.getType()),
                new MysqlQueryViewFunction(JdbcMetadataTreeView.VIEW_TYPE.getType()),
                new MysqlQueryTableDetailFunction(),
                new MysqlAllMetadataEditorKeyFunction("allMetadataEditorKey"),
                new MysqlColumnsEditorKeyFunction("columnsEditorKey"),
                new MysqlCharsetFunction()));
    }

    @Override
    public String getDriver(ConnectionInfo connectionInfo) {
        return "com.mysql.jdbc.Driver";
    }
}
